package tests.create;

import static org.junit.Assert.fail;
import model.Budget;
import model.BudgetFactory;
import model.Line;
import model.Subline;
import model.Transaction;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.workplicity.util.Helper;
import org.workplicity.worklet.WorkletContext;

import cc.test.bridge.BudgetInterface;
import cc.test.bridge.LineInterface;
import cc.test.bridge.SublineInterface;
import cc.test.bridge.TransactionInterface;

/*
 * 
 * Attempts to create a a budget with a basic transaction
 * 
 */

public class TransactionCreateTest00
{
	BudgetInterface budget;
	BudgetFactory factory;
	LineInterface line;
	SublineInterface subline;
	TransactionInterface trans;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception
	{
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception
	{
	}

	@Before
	public void setUp() throws Exception
	{
		factory = new BudgetFactory();
		budget = factory.create();
        budget.setName("Test budget");
        budget.setDescription("This is a test budget.");
        
        line = budget.createLine();
        budget.add(line);
        
        subline = line.createSubline();
		line.add((SublineInterface) subline);
	}

	@After
	public void tearDown() throws Exception
	{
		Helper.delete( (Transaction)trans, "Transactions", WorkletContext.getInstance());
		Helper.delete( (Subline)subline, "Sublines", WorkletContext.getInstance());
		Helper.delete( (Line)line, "Lines", WorkletContext.getInstance());
		Helper.delete( (Budget)budget, "Budgets", WorkletContext.getInstance());
	}

	@Test
	public void test()
	{
		try
        {
			
			 trans = subline.createTransaction();
			 subline.add(trans);
			 
			 Boolean successful = budget.commit();
			 
			 if(!successful)
			 {
				fail("Could not create/add subline");
			 }
        }
		catch(Exception e)
		{
			fail("Subline create failed with exception " + e);
		}
	}
}